﻿using Basic.FormDAL;
using Basic.FormModel.DB;
using Basic.FormModel.UserRecord;

namespace Basic.FormCollect.lmpl
{
    internal class UserFormRecordCollect : IUserFormRecordCollect
    {
        private readonly IUserFormRecordDAL _BasicDAL;

        public UserFormRecordCollect ( IUserFormRecordDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public UserRecord Find ( long formId, string linkType, long linkPk )
        {
            return this._BasicDAL.Find<UserRecord>(a => a.FormId == formId && a.LinkType == linkType && a.LinkPK == linkPk);
        }
        public UserRecordDto Sync ( UserRecordAdd record )
        {
            long recordId = this.FindRecordId(record.FormId, record.LinkType, record.LinkPK);
            if ( recordId == 0 )
            {
                return this._BasicDAL.Add(record);
            }
            return this._BasicDAL.Set(recordId, record.EmpId, record.Items);
        }
        public bool CheckIsNull ( long formId, string linkType, long linkPK )
        {
            var res = this._BasicDAL.Find(a => a.FormId == formId && a.LinkType == linkType && a.LinkPK == linkPK, a => new
            {
                a.IsNull
            });
            return res == null || res.IsNull;
        }
        public long FindRecordId ( long formId, string linkType, long linkPk )
        {
            return this._BasicDAL.Find(a => a.FormId == formId && a.LinkType == linkType && a.LinkPK == linkPk, a => a.Id);
        }
        public void Delete ( long id )
        {
            this._BasicDAL.Delete(id);
        }

        public DBUserFormRecord Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }
    }
}
